<?php

namespace Common\Controller;
class AddonController extends \Common\Controller\BaseController
{
    protected $addon_relative_path;
    protected $addon_path;

    public function _initialize()
    {
        parent::_initialize();
        $classname  = get_called_class();
        $h          = new \ReflectionClass($classname);
        $filename   = $h->getFileName();
        $addon_name = explode('\\', $classname)[0];
        if (!M('Addon')->where(['name' => $addon_name, 'status' => 1])->count() || strtolower($addon_name) == 'addon') {
            $this->error('非法访问', entry_url('index/index/index'));
        }

        // 计算相对地址
        $root                      = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $_SERVER['DOCUMENT_ROOT']);
        $relative                  = str_replace(DIRECTORY_SEPARATOR, '/', str_replace($root, '', realpath($filename)));
        $this->addon_relative_path = rtrim(explode(str_ireplace('\\', '/', $classname), $relative)[0], '/') . '/' . $addon_name;
        $this->addon_relative_path = '/' . ltrim($this->addon_relative_path, '/');
        define('__ADDON__', $this->addon_relative_path); // 常量写入
        // 计算包含地址
        $this->addon_path = preg_replace('!^' . preg_quote(rtrim(__ROOT__, '/')) . '/!is', '', $this->addon_relative_path);
        define('ADDON_PATH', $this->addon_path);
        // 加载常规配置文件
        $common_conf = $this->addon_path . '/Conf/config.php';
        if (is_file($common_conf)) C(require $common_conf);
        if (I('limit'))
            C('LIST_NUMBERS', I('limit')); // 自定分页数
    }

    /**
     * 列表显示
     * @param $listData
     * @param string $tpl
     */
    protected function displayList($listData, $tpl = './Template/Common/list.html')
    {
        if ($listData['toolbarTpl']) {
            $this->assign('toolbarTpl', $listData['toolbarTpl']);
            // 默认事件控制模版
            $events = array(
                'onToolbar'  => './Template/Common/onToolbar.html',
                'onCheckbox' => './Template/Common/onCheckbox.html',
                'onEdit'     => './Template/Common/onEdit.html',
                'onRow'      => './Template/Common/onRow.html',
                'onTool'     => './Template/Common/onTool.html',
                'onSort'     => './Template/Common/onSort.html',
            );
            foreach ($events as $key => $event_tpl) {
                if (!is_file($event_tpl))
                    unset($events[$key]);
            }
            $this->assign($events);
            foreach ($listData as $key => $v) {
                if (preg_match('!^on[A-Z]!', $key)) {
                    $this->assign($key, $v);
                    unset($listData[$key]);
                }
            }
            unset($listData['toolbarTpl']);
        }
        $opt         = array(
            'limit' => C('LIST_NUMBERS') ? C('LIST_NUMBERS') : 10,
            'id'    => 'dataTable',
        );
        $opt         = array_merge($opt, $listData);
        $opt['cols'] = array($listData['cols']);
        if (!$tpl) $tpl = 'Common/list';
        $this->assign('option', $opt);
        $this->display($tpl);
    }
}